package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: BlogDao
 * @Author 郑雨杭
 * @Package model
 * @Date 2024/7/29 17:03
 * @description:
 */
public class BlogDao {
    public void insert(Blog blog){
        Connection connection = null;
        PreparedStatement statement=null;
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into blog values(null,?,?,now(),?);";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3,blog.getUserid());
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }

    public List<Blog> findAll(){
        Connection connection = null;
        PreparedStatement statement=null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog order by post_time desc ";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            List<Blog> ret = new ArrayList<>();
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogid(resultSet.getInt("blogid"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
                if (content.length()>100){
                    content=content.substring(0, 100)+"……";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("post_time"));
                blog.setUserid(resultSet.getInt("userid"));
                ret.add(blog);
            }
            return ret;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
    }

    public Blog findById(int id){
        Connection connection = null;
        PreparedStatement statement=null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog where blogid = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogid(resultSet.getInt("blogid"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("post_time"));
                blog.setBlogid(resultSet.getInt("userid"));
                return blog;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
    }

    public void delete(int id){
        Connection connection = null;
        PreparedStatement statement=null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from blog where blogid = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
}
